﻿using Common;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bussiness
{
    public class DAL
    {
        public List<Device> GetDeviceList()
        {
            List<Device> deviceList = new List<Device>();
            List<Cfg_Row> cfg_row_list = XML.Convert2Row("设备管理");
            foreach(var cfg_row in cfg_row_list)
            {
                Device device = new Device();
                device.Guid = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("标识符")).value;
                device.DevCode = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备编号")).value;
                device.DevName = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备名称")).value;
                device.IP = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备IP")).value;
                device.Port = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备端口")).value.ToInt();
                device.DevTypeGuid = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备类型标识符")).value;
                DeviceType deviceType = GetDeviceType(device.DevTypeGuid);
                if (deviceType != null)
                {
                    device.DevTypeCode = deviceType.DeviceTypeCode;
                    device.DevTypeName = deviceType.DeviceTypeName;
                }

                device.EditTime= cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("更新时间")).value;
                deviceList.Add(device);
            }

            return deviceList;
        }

        public Device GetDevice(string devGuid)
        {
            List<Device> deviceList = GetDeviceList();
            Device device=deviceList.FirstOrDefault(p=>p.Guid.Equals(devGuid));
            return device;
        }

        public Result SaveDevice(Device device,bool isADD)
        {
            Result result = new Result();
            if (isADD)
            {
                device.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                device.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            
            Cfg_Row cfg_Row = new Cfg_Row();
            cfg_Row.FieldList = new List<Cfg_Field>();
            Cfg_Field cfg_Field = new Cfg_Field();
            if (isADD)
            {
                cfg_Field.name = "标识符";
                cfg_Field.value = device.Guid;
                cfg_Row.FieldList.Add(cfg_Field);
            }

            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备名称";
            cfg_Field.value = device.DevName;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备编号";
            cfg_Field.value = device.DevCode;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备IP";
            cfg_Field.value = device.IP;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备端口";
            cfg_Field.value = device.Port.ToString();
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备类型标识符";
            cfg_Field.value = device.DevTypeGuid;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "更新时间";
            cfg_Field.value = device.EditTime;
            cfg_Row.FieldList.Add(cfg_Field);

            try
            {
                if (isADD)
                {
                    XML.Add("设备管理", cfg_Row);
                }
                else
                {
                    XML.Update("设备管理", "设备编号", cfg_Row);
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public Result DelDevice(string devGuid)
        {
            Result result = new Result();

            try
            {
                XML.Delete("设备管理",new Cfg_Field() { name="标识符",value= devGuid });
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public List<DeviceType> GetDeviceTypeList()
        {
            List<DeviceType> deviceTypeList = new List<DeviceType>();
            List<Cfg_Row> cfg_row_list = XML.Convert2Row("设备类型管理");
            foreach (var cfg_row in cfg_row_list)
            {
                DeviceType deviceType = new DeviceType();
                deviceType.Guid = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("标识符")).value;
                deviceType.DeviceTypeCode = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备类型编号")).value;
                deviceType.DeviceTypeName = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备类型名称")).value;
                deviceType.CaptureFreq = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("采集频率")).value.ToInt();
                deviceTypeList.Add(deviceType);
            }

            return deviceTypeList;
        }

        public List<Cfg_Field> GetDeviceTypeFieldList()
        {
            List<DeviceType> deviceTypeList = GetDeviceTypeList();
            List<Cfg_Field> fieldList = new List<Cfg_Field>();
            foreach (var deviceType in deviceTypeList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = deviceType.DeviceTypeName;
                cfg_Field.value = deviceType.Guid;
                fieldList.Add(cfg_Field);
            }

            return fieldList;
        }

        public DeviceType GetDeviceType(string devTypeGuid)
        {
            List<DeviceType> deviceTypeList = GetDeviceTypeList();
            DeviceType deviceType = deviceTypeList.FirstOrDefault(p => p.Guid.Equals(devTypeGuid));
            return deviceType;
        }

        public Result SaveDeviceType(DeviceType deviceType, bool isADD)
        {
            Result result = new Result();
            Cfg_Row cfg_Row = new Cfg_Row();
            cfg_Row.FieldList = new List<Cfg_Field>();
            Cfg_Field cfg_Field = new Cfg_Field();
            cfg_Field.name = "标识符";
            cfg_Field.value = deviceType.Guid;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备类型名称";
            cfg_Field.value = deviceType.DeviceTypeName;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备类型编号";
            cfg_Field.value = deviceType.DeviceTypeCode;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "采集频率";
            cfg_Field.value = deviceType.CaptureFreq.ToString2();
            cfg_Row.FieldList.Add(cfg_Field);

            try
            {
                if (isADD)
                {
                    XML.Add("设备类型管理", cfg_Row);
                }
                else
                {
                    XML.Update("设备类型管理", "设备类型编号", cfg_Row);
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public Result DelDeviceType(string deviceTypeGuid)
        {
            Result result = new Result();

            try
            {
                XML.Delete("设备类型管理", new Cfg_Field() { name = "标识符", value = deviceTypeGuid });
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public List<Cfg_Field> GetDevicePOITypeFieldList()
        {
            List<Cfg_Field> fieldList = new List<Cfg_Field>();
            Cfg_Field cfg_Field = new Cfg_Field();
            cfg_Field.name = "int";
            cfg_Field.value = "1";
            fieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "short";
            cfg_Field.value = "2";
            fieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "long";
            cfg_Field.value = "3";
            fieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "float";
            cfg_Field.value = "4";
            fieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "string";
            cfg_Field.value = "5";
            fieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "bit";
            cfg_Field.value = "6";
            fieldList.Add(cfg_Field);
            return fieldList;
        }

        public List<DevicePOI> GetDevicePOIList(string devTypeGuid)
        {
            List<DevicePOI> devicePOIList = new List<DevicePOI>();
            List<Cfg_Row> cfg_row_list = XML.Convert2Row("设备采点管理");
            foreach (var cfg_row in cfg_row_list)
            {
                DevicePOI devicePOI = new DevicePOI();
                devicePOI.Guid = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("标识符")).value;
                devicePOI.DevTypeGuid = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("设备类型标识符")).value;
                DeviceType deviceType = GetDeviceType(devicePOI.DevTypeGuid);
                if (deviceType != null)
                {
                    devicePOI.DevTypeCode = deviceType.DeviceTypeCode;
                    devicePOI.DevTypeName = deviceType.DeviceTypeName;
                }

                devicePOI.DevPOIName = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("点位名称")).value;
                devicePOI.DevPOIAlias = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("点位别名")).value;
                devicePOI.DevPOIType = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("点位类型")).value.ToInt();

                switch (devicePOI.DevPOIType)
                {
                    case 1:
                        devicePOI.DevPOITypeName = "int";
                        break;
                    case 2:
                        devicePOI.DevPOITypeName = "short";
                        break;
                    case 3:
                        devicePOI.DevPOITypeName = "long";
                        break;
                    case 4:
                        devicePOI.DevPOITypeName = "float";
                        break;
                    case 5:
                        devicePOI.DevPOITypeName = "string";
                        break;
                }

                devicePOI.Address = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("点位地址")).value;
                devicePOI.Enable = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("是否采集")).value.ToBool();
                devicePOIList.Add(devicePOI);
            }

            if (!string.IsNullOrEmpty(devTypeGuid))
            {
                devicePOIList = devicePOIList.Where(p => p.DevTypeGuid.Equals(devTypeGuid)).ToList();
            }

            devicePOIList = devicePOIList.OrderBy(p=>p.DevTypeCode).ToList();
            return devicePOIList;
        }

        public DevicePOI GetDevicePOI(string poiGuid)
        {
            List<DevicePOI> devicePOIList = GetDevicePOIList(string.Empty);
            DevicePOI devicePOI = devicePOIList.FirstOrDefault(p => !string.IsNullOrEmpty(p.Guid) && p.Guid.Equals(poiGuid));
            return devicePOI;
        }

        public Result SaveDevicePOI(DevicePOI devicePOI, bool isADD)
        {
            Result result = new Result();
            Cfg_Row cfg_Row = new Cfg_Row();
            cfg_Row.FieldList = new List<Cfg_Field>();
            Cfg_Field cfg_Field = new Cfg_Field();
            cfg_Field.name = "标识符";
            cfg_Field.value = devicePOI.Guid;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "设备类型标识符";
            cfg_Field.value = devicePOI.DevTypeGuid;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "点位名称";
            cfg_Field.value = devicePOI.DevPOIName;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "点位别名";
            cfg_Field.value = devicePOI.DevPOIAlias;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "点位类型";
            cfg_Field.value = devicePOI.DevPOIType.ToString2();
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "点位地址";
            cfg_Field.value = devicePOI.Address;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "是否采集";
            cfg_Field.value = devicePOI.Enable?"1":"0";
            cfg_Row.FieldList.Add(cfg_Field);

            try
            {
                if (isADD)
                {
                    XML.Add("设备采点管理", cfg_Row);
                }
                else
                {
                    XML.Update("设备采点管理", "标识符", cfg_Row);
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public Result DelDevicePOI(string poiGuid)
        {
            Result result = new Result();

            try
            {
                List<Cfg_Field> primaryFieldList = new List<Cfg_Field>
                {
                    new Cfg_Field() { name = "标识符", value = poiGuid }
                };
                XML.Delete("设备采点管理", primaryFieldList);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }

        public Config GetConfig()
        {
            Config config = new Config();
            List<Cfg_Row> cfg_row_list = XML.Convert2Row("基础设置");
            foreach (var cfg_row in cfg_row_list)
            {
                config.WebsocketURL= cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("接口地址")).value;
                config.LogPath = cfg_row.FieldList.FirstOrDefault(p => p.name.Equals("日志保存路径")).value;
            }

            return config;
        }

        public Result SaveConfig(Config config)
        {
            Result result = new Result();
            Cfg_Row cfg_Row = new Cfg_Row();
            cfg_Row.FieldList = new List<Cfg_Field>();
            Cfg_Field cfg_Field = new Cfg_Field();
            cfg_Field.name = "参数编号";
            cfg_Field.value = "001";
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "接口地址";
            cfg_Field.value = config.WebsocketURL;
            cfg_Row.FieldList.Add(cfg_Field);
            cfg_Field = new Cfg_Field();
            cfg_Field.name = "日志保存路径";
            cfg_Field.value = config.LogPath;
            cfg_Row.FieldList.Add(cfg_Field);

            try
            {
                XML.Update("基础设置", "参数编号", cfg_Row);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.ToString();
            }

            return result;
        }
    }
}
